<template>
  <div class="app-container">
    <!-- v-permission="['alarmSet:alarm:read']"
    v-permission="['alarmSet:alarm:create']"
    v-permission="['alarmSet:alarm:update']"
    v-permission="['alarmSet:alarm:delete']" -->
    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline filter">
      <el-form-item label="名称">
      <el-input v-model.trim="listQuery.fariname" clearable class="filter-item" placeholder="请输入空气项目名称"/>
      </el-form-item>
      <el-form-item>
         <el-button round size="medium" class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" @click="handleFilter">查找</el-button>
      <el-button round size="medium" class="filter-item" :style="{background:buttonColor.add ,color:buttonColor.color}" @click="handleCreate">添加</el-button>
      </el-form-item>
      </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeight" v-loading="listLoading" :data="list" :header-cell-style="tableColor" element-loading-text="正在查询中。。。" border fit highlight-current-row>
       <el-table-column align="center" :width="buttonColor.operationTwo" label="操作" class-name="small-padding">
        <template slot-scope="scope">
           <div class="operationButton">
            <div :style="{color:buttonColor.edit,cursor:'pointer'}" @click="handleUpdate(scope.row)">
            <img src="../../../public/images/edit.png" />
            编辑
          </div>
          <div :style="{color:buttonColor.delete,cursor:'pointer'}" @click="handleDelete(scope.row)">
            <img src="../../../public/images/del.png" />
            删除
          </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序" type="index"/>
      <el-table-column align="left" label="报警编号" prop="fairitem"/>
      <el-table-column align="left" label="项目名称" prop="fariname"/>
      <el-table-column align="left" label="字段名" prop="ffield"/>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="dialogFormVisible" width="auto">
      <template slot="title">
      <div :style="titleColor">{{textMap[dialogStatus]}}</div>
    </template>
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="80px" style="width: 400px; margin: 0 30px;">
        <el-form-item class="required" label="报警编号" prop="fairitem">
          <el-input v-model="dataForm.fairitem" :disabled="dialogStatus=='update'"/>
        </el-form-item>
        <el-form-item class="required" label="项目名称" prop="fariname">
          <el-input v-model="dataForm.fariname"/>
        </el-form-item>
        <el-form-item class="required" label="字段名" prop="ffield">
          <el-input v-model="dataForm.ffield"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible = false">取消</el-button>
        <el-button round v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button round v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listAlarm, createAlarm, updateAlarm, deleteAlarm } from '@/api/alarm'
import Pagination from '@/components/Pagination'
import moment from 'moment/moment'
import { tableColor,titleColor,buttonColor} from '@/filters/index'

export default {
  name: 'Alarm',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fariname: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        id: undefined,
        fairitem: undefined,
        fariname: undefined,
        ffield: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableColor: tableColor,
      titleColor: titleColor,
      buttonColor:buttonColor,
      rules: {
        fairitem: [
          { required: true, message: '报警编号不能为空', trigger: 'blur' }
        ],
        fariname: [
          { required: true, message: '空气项目名称不能为空', trigger: 'blur' }
        ],
        ffield: [
          { required: true, message: '字段名不能为空', trigger: 'blur' }
        ]
      },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatStatus: function(row, column) {
      return row.fsiteState === 1 ? '启用' : '禁用'
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listAlarm(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fairitem: undefined,
        fariname: undefined,
        ffield: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createAlarm(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加空气字典成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updateAlarm(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新管理员成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该空气字典', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAlarm(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除空气字典成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.msg
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
